package com.navi.loaddata.loadDao;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.oracleEntity.fileMapping.Cell2ChipMappingT;
import com.navi.loaddata.core.oracleEntity.fileMapping.CellArrayCfMappingT;
import com.navi.loaddata.core.oracleEntity.fileMapping.CellArrayCfMappingTModel;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.loadService.impl.arrayCfMappingData.LoadArrayCfMappingInfoService;
import com.navi.loaddata.loadService.impl.arrayCfMappingData.ProcessArrayCfMappingDataService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class SaveMappingDao {
    private static final Logger logger = LoggerFactory.getLogger(SaveMappingDao.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private OracleRepositoryImpl dao3;

    public void saveAryCfMapping(List<CellArrayCfMappingT> cellList1000, StringBuffer glassIds) throws Exception{
        Session session = dao3.getEntityManagerSession();
        try {
            String hqlDelete = "Delete FROM CellArrayCfMappingT where arrayComponentId in(" + glassIds + ")";
            Query qdelete = session.createQuery(hqlDelete);
            qdelete.executeUpdate();
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_ACF_MAPPING,e);
        }
        try {
            dao3.saveAll(cellList1000);
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_ACF_MAPPING,e);
        }
    }

    public void saveCellMapping(List<Cell2ChipMappingT> cellList1000, StringBuffer glassIds) throws Exception {
        Session session = dao3.getEntityManagerSession();
        try {
            String hqlDelete = "Delete FROM Cell2ChipMappingT where chipId in(" + glassIds + ")";
            Query qdelete = session.createQuery(hqlDelete);
            qdelete.executeUpdate();
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_CELL_MAPPING,e);
        }
        try {
            dao3.saveAll(cellList1000);
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_CELL_MAPPING,e);
        }
    }
}
